package cn.tedu.egmvc1.controller;

import cn.tedu.egmvc1.base.response.JsonResult;
import cn.tedu.egmvc1.mapper.UserMapper;
import cn.tedu.egmvc1.pojo.dto.UserAddParam;
import cn.tedu.egmvc1.pojo.dto.UserListQuery;
import cn.tedu.egmvc1.pojo.dto.UserUpdateParam;
import cn.tedu.egmvc1.pojo.entity.User;
import cn.tedu.egmvc1.pojo.vo.UserListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/v1/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @PostMapping("add")
    public JsonResult addUser(@PathVariable UserAddParam userAddParam){

        log.debug("userAddParam"+userAddParam);
        User user = new User();
        BeanUtils.copyProperties(userAddParam,user);
        userMapper.insertUser(user);
        return JsonResult.ok();
    }
    @GetMapping("list")
    public JsonResult userList(UserListQuery userListQuery){
        log.debug("userListQuery"+userListQuery);
        String username = userListQuery.getUsername();
        String password = userListQuery.getPassword();
        List<UserListVO> UserListVO = userMapper.selectUser(username, password);
        return JsonResult.ok(UserListVO);
    }
    @ApiOperation("删除用户")
    @ApiOperationSupport(order = 10)
    @PostMapping("del")
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Integer")
    public JsonResult delUser(@Range(min = 1,message = "参数id不能小于0")Integer id){
        if (id<0){
            throw new IllegalArgumentException("参数id不能小于0");
        }
        log.debug("id = "+id);
        userMapper.delUser(id);
        return JsonResult.ok();
    }
    @PostMapping("update")
    public JsonResult updateUser(UserUpdateParam userUpdateParam){
        log.debug("userUpdateParam"+userUpdateParam);
        User user = new User();
        BeanUtils.copyProperties(userUpdateParam,user);
        userMapper.updateUser(user);
        return JsonResult.ok();
    }
}
